<?php

/**
 * This is the model class for table "tickets".
 *
 * The followings are the available columns in table 'tickets':
 * @property integer $id
 * @property string $type
 * @property string $price
 * @property string $remain_quantity
 * @property string $temp_quantity
 * @property string $ticket_code
 * @property string $total
 * @property integer $event_id
 *
 * The followings are the available model relations:
 * @property Booked[] $bookeds
 * @property Events $event
 */
class Ticket extends CActiveRecord
{
	public $fileImage;    
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Ticket the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tickets';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('event_id, quantity', 'numerical', 'integerOnly'=>true),
//			array('type, price, quantity, ticket_code', 'required'),
			array('type_of_ticket, ticket_code', 'length', 'max'=>255),
			array('price, quantity', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type_of_ticket,description_of_ticket,ticket_code_org,date,seat_map,ranking, price, temp_quantity, remain_quantity, quantity, ticket_code, event_id,created_by,updated_by,updated_date', 'safe'),
			array('fileImage', 'file', 'on' => 'create,update',
				'allowEmpty' => true,
				'types' => 'jpg,gif,png',
				'wrongType' => Yii::t('lang', 'Only jpg,gif,png are allowed.'),
                           'maxSize' => MyFunctionCustom::maxFileUpload(), // 5MB
                           'tooLarge' => Yii::t('lang', 'The file was larger than 500 KB.'),
                           ),            
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'booked' => array(self::HAS_MANY, 'Booked', 'ticket_id'),
			'event' => array(self::BELONGS_TO, 'Event', 'event_id'),
			'update_user' => array(self::BELONGS_TO,'Users','updated_by'),
			'create_user' => array(self::BELONGS_TO,'Users','created_by')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'type' => 'Type',
			'price' => 'Price',
			'remain_quantity' => 'Remain Quantity',
			'ticket_code' => 'Ticket Code',
			'total' => 'Total',
			'event_id' => 'Event',
			'ranking' => 'Ticket Type',
			'temp_quantity' => 'Add/Sub Quantity'
		);
	}

	// protected function afterFind() {


	// 	if($this->scenario == "update1") {
	// 		$result = str_replace(array('"', '[', ']'), array('','',''), $this->temp_quantity);
	// 		// $temp_quantity = "Chuoi da xu ly cua ".$this->temp_quantity;

	// 		$this->temp_quantity = $result;

	// 	}

	// 	parent::afterFind();
	// }

	public function getTempQuantityText() {
		return  str_replace(array('"', '[', ']'), array('','',''), $this->temp_quantity);
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('remain_quantity',$this->remain_quantity,true);
		$criteria->compare('ticket_code',$this->ticket_code,true);
		$criteria->compare('total',$this->total,true);
		$criteria->compare('event_id',$this->event_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    /**
     * <Jason>
     * <Email: pmhai90@gmail.com>
     * @return string
     */
    public static function loadHtml(){
        $html = '';
        
        $html .= '
    <div class="span12" style="margin-top: 10px;">
        <!--PAGE CONTENT BEGINS-->
        <div class="control-group">
            <label class="control-label" for="Ticket_ranking">Ticket type</label>            <div class="controls">
                <input size="60" maxlength="255" class="span5 quantity" value="0" placeholder="0-&gt;xx" name="Ticket[type_of_ticket][]" id="Ticket_ranking" type="text" />                <span class="help-inline"><div class="errorMessage" id="Ticket_ranking_em_" style="display:none"></div></span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="Ticket_description_of_ticket">Description Of Ticket</label>            <div class="controls">
                <input size="60" maxlength="255" class="span5" placeholder="VIP Seat, Luxury Seat, Normal Seat. " name="Ticket[description_of_ticket][]" id="Ticket_description_of_ticket" type="text" />                <span class="help-inline"><div class="errorMessage" id="Ticket_description_of_ticket_em_" style="display:none"></div></span>
            </div>
        </div>
        
        <div class="control-group">
            <label class="control-label" for="Ticket_price">Price</label>            <div class="controls">
                <input size="60" maxlength="255" class="price" required="required" placeholder="Price" name="Ticket[price][]" id="Ticket_price" type="text" /> / VNĐ
                <span class="help-inline"><div class="errorMessage" id="Ticket_price_em_" style="display:none"></div></span>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label" for="Ticket_quantity">Quantity</label>            <div class="controls">
                <input size="60" maxlength="255" class="span5 quantity" required="required" placeholder="Total" name="Ticket[quantity][]" id="Ticket_quantity" type="number" />                <span class="help-inline"><div class="errorMessage" id="Ticket_quantity_em_" style="display:none"></div></span>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label" for="Ticket_ticket_code_org">Ticket Code Org</label>            <div class="controls">
                <input size="60" maxlength="255" class="span5" placeholder="X143234#1232" name="Ticket[ticket_code_org][]" id="Ticket_ticket_code_org" type="text" />                <span class="help-inline"><div class="errorMessage" id="Ticket_ticket_code_org_em_" style="display:none"></div></span>
            </div>
        </div>
        
        <div class="control-group" style="display:none;">
            <label class="control-label" for="Ticket_seat_map">Seat Map</label>            <div class="controls">
                <select class="span5" id="seat1" placeholder="Seat Map" name="Ticket[seat_map][]">
<option value="0">No</option>
<option value="1">Yes</option>
</select>                <span class="help-inline"><div class="errorMessage" id="Ticket_seat_map_em_" style="display:none"></div></span>
            </div>
        </div>    

        <div class="control-group file1 hide1">
            <label class="control-label" for="Ticket_fileImage">File Image</label>            
            <div class="controls">
            	
                
                
            </div>
        </div>
    
        <div  ="control-group">
        	<label class="control-label">Delete</label>
        	<div class="controls">
            	<a style="margin-left: 30px;" href="#" class="removeAddMore btn btn-primary">
            	<i class="icon-trash"></i>Remove
            	</a>
            	<script type="text/javascript">
            		$(document).ready(function(){
            			$(".removeAddMore").click(function(){
            				var a = $(this).parent().parent().parent().attr("class");

					        $(this).parent().parent().parent().remove();

					        fnUpdateNoTr();
            			});
						$("select#seat1").change(function(){
					        var parent = $(this).parent().parent().parent();
					        // console.log(parent);
					        var a = $(this).val();
					        if (a === "1") {
					            $(parent).find(".file1").removeClass("hide1");
					        }
					        else{
					            $(parent).find(".file1").addClass("hide1");
					        }
					    });
            		});
            	</script>       
        	</div>
        </div>
    </div>';
        
    return $html;
    }    
}